import asyncio


class CountDownLatch:
    def __init__(self, count):
        self.count = count
        self.event = asyncio.Event()

    async def count_down(self):
        self.count -= 1
        if self.count <= 0:
            self.event.set()

    async def await_(self):
        await self.event.wait()


# 示例用法
async def worker(latch: CountDownLatch):
    print("Worker started")
    await latch.count_down()
    print("Worker finished")


async def main():
    latch = CountDownLatch(3)
    tasks = [worker(latch) for _ in range(3)]
    await asyncio.gather(*tasks)
    await latch.await_()
    print("All workers finished")


if __name__ == "__main__":
    asyncio.run(main())
